import type { AxiosProgressEvent } from 'axios';

import type { UploadFileParams } from '#/request';

import { deleteLogo, getLogoBase64String, uploadLogo } from '@/api/file';
import { setAppDescription, setAppTitle } from '@/api/system/admin/settings';
import logoImg from '@/assets/images/logo.png';
import { useI18n } from '@/hooks/web/useI18n';
import { store } from '@/store';
import { getAppEnvConfig } from '@/utils/env';
import { defineStore } from 'pinia';

const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();

interface PreferenceState {
  /** 应用LOGO */
  logoSrc?: null | string;
  /** 应用标题 */
  appTitle?: null | string;
  /** 应用描述 */
  appDescription?: null | string;
  /** 应用子描述 */
  appSubdescription?: null | string;
}
export const usePreferenceStore = defineStore('preference', {
  state: (): PreferenceState => ({
    logoSrc: undefined,
    appTitle: undefined,
    appDescription: undefined,
    appSubdescription: undefined,
  }),
  getters: {
    /** 获取LOGO */
    getLogo(state): string {
      return state.logoSrc || logoImg;
    },
    /** 获取应用标题 */
    getAppTitle(state): string {
      return state.appTitle || VITE_GLOB_APP_TITLE;
    },
    /** 获取应用描述 */
    getAppDescription(state): string {
      const { t } = useI18n();
      return state.appDescription || t('sys.login.signInTitle');
    },
    /** 获取应用子描述 */
    getAppSubdescription(state): string {
      const { t } = useI18n();
      return state.appSubdescription || t('sys.login.signInDesc');
    },
  },
  actions: {
    /** 重置LOGO */
    async resetLogo() {
      const result = await deleteLogo();
      if (result.Succeeded) {
        this.setLogo(undefined);
      }
      return result;
    },
    /** 上传LOGO */
    async uploadLogo(params: UploadFileParams, onUploadProgress: (progressEvent: AxiosProgressEvent) => void) {
      const result = await uploadLogo(params, onUploadProgress);
      const logo = await getLogoBase64String();
      if (logo) {
        this.setLogo(logo);
      }
      return result;
    },
    /** 设置LOGO */
    setLogo(logo?: null | string) {
      this.logoSrc = logo;
      // #region 系统网站图标处理
      let $favicon = document.querySelector('link[rel="icon"]') as HTMLLinkElement;
      if ($favicon !== null) {
        $favicon.href = this.getLogo;
      } else {
        $favicon = document.createElement('link');
        $favicon.rel = 'icon';
        $favicon.href = this.getLogo;
        document.head.appendChild($favicon);
      }
      // #endregion
    },
    /** 设置应用标题 */
    async setAppTitle(title?: null | string, sendRequest = true) {
      if (sendRequest) {
        const result = await setAppTitle(title);
        if (result.Succeeded) {
          this.appTitle = title;
        }
        return result.Succeeded;
      } else {
        this.appTitle = title;
        return true;
      }
    },
    /** 设置应用描述 */
    async setAppDescription(description?: null | string, sendRequest = true) {
      if (sendRequest) {
        const result = await setAppDescription({
          Description: description,
          Subdescription: this.appSubdescription,
        });
        if (result.Succeeded) {
          this.appDescription = description;
        }
        return result.Succeeded;
      } else {
        this.appDescription = description;
        return true;
      }
    },
    /** 设置应用子描述 */
    async setAppSubdescription(subdescription?: null | string, sendRequest = true) {
      if (sendRequest) {
        const result = await setAppDescription({
          Description: this.appDescription,
          Subdescription: subdescription,
        });
        if (result.Succeeded) {
          this.appSubdescription = subdescription;
        }
        return result.Succeeded;
      } else {
        this.appSubdescription = subdescription;
        return true;
      }
    },
  },
  sync: true,
});

// Need to be used outside the setup
export function usePreferenceStoreWithOut() {
  return usePreferenceStore(store);
}
